package coroutines.job

import coroutines.interceptor.ExceptionInterceptor
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlin.coroutines.EmptyCoroutineContext

class CoroutineCancelTest {
}

fun main() {
    runBlocking(context =  ExceptionInterceptor()) {
        val job0 = launch(context = EmptyCoroutineContext + ExceptionInterceptor()) {
            repeat(1000) { i ->
                println("job0: I'm sleeping $i ...")
                delay(500L)
            }
        }

        delay(1300L) // 延迟一段时间
        println("main: job0  I'm tired of waiting!")
        job0.cancel() // 取消该作业
        job0.join() // 等待作业执行结束
        println("main: job0 Now I can quit.")


        val job1 = launch {
            for (i in 0 until 100) {
                println("job1: I'm sleeping $i ...")
                delay(500L)
            }
        }

        delay(1300L) // 延迟一段时间
        println("main: job1  I'm tired of waiting!")
        job1.cancel() // 取消该作业
        job1.join() // 等待作业执行结束
        println("main: job1 Now I can quit.")
    }
}